#!/usr/bin/env bash
# desc start - wget https://hello.fe80.cn/bash/install.sh && bash install.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
cur_dir=$(pwd)

Color_Text()
{
    echo -e " \e[0;$2m$1\e[0m"
}

Echo_Red()
{
    echo $(Color_Text "$1" "31")
}

Echo_Green()
{
    echo $(Color_Text "$1" "32")
}

Echo_Yellow()
{
    echo $(Color_Text "$1" "33")
}

Echo_Blue()
{
    echo $(Color_Text "$1" "34")
}

# Check if user is root
if [ $(id -u) != "0" ]; then
    Echo_Red "Error: 请使用root权限"
    exit 1
fi

cd ~

read -p "[+input]: 是否更新系统? y/n：" callable1
if [ "$callable1" = "y" ]; then
    Echo_Blue "[+] 即将更新系统 ..."
    yum update -y
    Echo_Green "[+] 更新系统完成 ..."
fi

if [[ `command -v git` = "" ]];then
    Echo_Blue "[+] 正在安装基础包 ..."
    yum install -y git lrzsz npm bind-utils
    yum install -y epel-release
    yum install -y tcping screen
fi

if [[ `command -v go` = "" ]];then
    Echo_Blue "[+] 正在安装golang ..."
    yum install -y golang
    go env -w GO111MODULE=on
    go env -w GOPROXY=https://goproxy.cn,direct
fi

read -p "[+input]: 是否安装LNMP? y/n：" callable2
if [ "$callable2" = "y" ]; then
    #git clone https://github.com/licess/lnmp
    git clone https://gitee.com/licess/lnmp.git

    cd lnmp

    cat > lnmp.conf<<EOF
Download_Mirror='https://up.fe80.cn/soft'
##Download_Mirror='https://soft.vpser.net'##
##Download_Mirror='https://upyun.vpser.net'##
Nginx_Modules_Options=''
PHP_Modules_Options=''

##MySQL/MariaDB database directory##
MySQL_Data_Dir='/usr/local/mysql/var'
MariaDB_Data_Dir='/usr/local/mariadb/var'
##Default website home directory##
Default_Website_Dir='/home/wwwroot/default'
Enable_Nginx_Openssl='y'
Enable_PHP_Fileinfo='n'
Enable_Nginx_Lua='y'
Enable_Swap='y'

Redis_Auto="y"
Mongo_Auto="y"
DBSelect="0"
InstallInnodb="y"
PHPSelect="11"
DB_Root_Password="root"
SelectMalloc="1"
LNMP_Auto="y"

Enable_Ngx_FancyIndex='n'
Enable_PHP_Exif='n'
Enable_PHP_Ldap='n'
Enable_PHP_Bz2='n'
Enable_PHP_Sodium='n'
Enable_PHP_Imap='n'
EOF

    bash install.sh

    Echo_Green "[+] 配置PHP.INI ..."
    sed -i 's/proc_open,proc_get_status,popen,//' /usr/local/php/etc/php.ini
    sed -i 's/readlink,//' /usr/local/php/etc/php.ini
    sed -i 's/exec,//' /usr/local/php/etc/php.ini #安装mongodb需要这个函数
    # 2021/03/01改为月切割，方便查找日志
    cat > /etc/logrotate.d/nginx<<EOF
/home/wwwlogs/*.log {
    su root root
    monthly
    dateext
    compress
    rotate 10
    notifempty
    sharedscripts
    postrotate
        kill -USR1 \`cat /usr/local/nginx/logs/nginx.pid\`
    endscript
}
EOF

    Echo_Green "[+] Settings the logrotate successfull ..."

    sed -i 's/cument_root\/\:\/tmp/cument_root\/\.\.\/\:\/tmp/' /usr/local/nginx/conf/fastcgi.conf
    #sed -i 's/\[mysqld\]/\[mysqld\]\ndefault_authentication_plugin=mysql_native_password/' /etc/my.cnf
    # XX
    sed -i 's/dir=\${vhostdir}:\/t/dir=\${vhostdir}\/..\/:\/t/' /bin/lnmp

    cat >  /home/wwwroot/default/index.html<<EOF
<html>
<head>
<meta charset="utf-8">
<title>网站建设中</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
</head>
<body>
<p align="center" style="font-size:32px; font-weight:bold;margin-top:50px;">网站建设中 敬请期待！</p>
</body>
</html>
EOF
    #phpmyadmin配置
    if [ -f "/home/wwwroot/default/phpmyadmin/config.inc.php" ];then
        if [[ `grep "localhost" /home/wwwroot/default/phpmyadmin/config.inc.php` != "" ]]; then
            sed -i 's/localhost/127.0.0.1/' /home/wwwroot/default/phpmyadmin/config.inc.php
            Echo_Green "[info] 配置phpmyadmin完成"
        fi
    fi
    Echo_Green "[+] Settings the default page successfull ..."

    /sbin/iptables -D INPUT -p tcp --dport 3306 -j DROP # 开启本机防火墙3306端口

    # 安装redis：要在最后安装
    if [ -f "include/redis.sh" ];then
        Echo_Green "[INFO]直接安装redis..."
        sed -i 's/Press_Start/#Press_Start/' include/redis.sh
    fi

    bash addons.sh install redis
    ln -sf /usr/local/redis/bin/redis-cli /usr/bin/
    if [ -f "/usr/local/redis/etc/redis.conf" ];then
        Echo_Green "[INFO]设置密码redis..."
        cat >> /usr/local/redis/etc/redis.conf<<EOF
requirepass 12345600
EOF
        Echo_Green "正在重启redis..."
        service redis restart
    fi
fi

#如果有IPv6开通IPv6登录
if [ `ip -6 address show|grep -c 240` -eq '1' ]; then
    # 检测IPv6是否可用，不可用，再配置
    Echo_Blue "[+]检测到您有IPv6地址，正在配置ssh..."
    ip -6 address show | grep inet6 | awk '{print $2}' | cut -d'/' -f1
    sed -i 's/\#AddressFamily/AddressFamily/' /etc/ssh/sshd_config
    sed -i 's/\#ListenAddress 0.0.0.0/ListenAddress 0.0.0.0/' /etc/ssh/sshd_config
    sed -i 's/\#ListenAddress ::/ListenAddress ::/' /etc/ssh/sshd_config
    Echo_Blue "[+]正在重启ssh..."
    /bin/systemctl restart sshd.service
fi
# 安装mysql
# if [ -z ${callable} ]; then
#     callable="n"
#     echo "未输入.默认值"${callable}
# else
#     echo "已经输入完成."${callable}
# fi

read -p "[+input]: 是否安装MySQL(8.0.32)? y/n：" callable3
if [ "$callable3" = "y" ]; then
    Echo_Blue "[+] 正在安装MySQL8.0.32"
    cd ~
    rm -rf mysql8032el8.sh
    wget https://hello.fe80.cn/bash/mysql8032el8.sh && bash mysql8032el8.sh
fi


# conda安装
# wget -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh
